#!/bin/bash
#target: install mariadb
#version: v1.1 add character-set-server=utf8mb4
#version: v1.3 change systemd service to start mysqld
#version: v1.4 change mysqld to mysqld_safe

# mariadb-10.2.x 只建议在centos7中安装。
set -u -e
tar_local="/root/mariadb-10.11.9-linux-systemd-x86_64.tar.gz" 
install_path="/usr/local/"
data_path="/data/mysql/"
tar_name="${tar_local##*/}"
tar_dir_name="${tar_name%.tar.gz}"


# shellcheck disable=SC1091
source /etc/os-release
# judge user if root

if [ $UID -ne 0 ];then
	echo -e "you need root to run this script! \E[1;32mexit.\E[0m"
	exit
fi

# first,create group and user
getent passwd mysql &> /dev/null  && echo "已有mysql用户，不再创建..."|| { groupadd mysql;useradd -u 3306  -r -g mysql -s /bin/false mysql; }

if [ "${ID}" = "ubuntu" ];then
	apt update
	apt install -y libaio-dev libncurses6 libtinfo6
	if [ ! -e  /lib/x86_64-linux-gnu/libncurses.so.5 ];then
	ln -s /lib/x86_64-linux-gnu/libncurses.so.6 /lib/x86_64-linux-gnu/libncurses.so.5
	fi
	if [ ! -e  /lib/x86_64-linux-gnu/libtinfo.so.5 ];then
	ln -s /lib/x86_64-linux-gnu/libtinfo.so.6 /lib/x86_64-linux-gnu/libtinfo.so.5
	fi
else 
	if [ "${ID}" = "rocky" ] || [ "${ID}" = "centos" ];then  
		if [ "${ID}" = "centos" ] && [ "${CENTOS_MANTISBT_PROJECT}" = "CentOS-7" ];then
			yum -y install ncurses numactl-libs libaio ncurses
		else
			yum -y install ncurses numactl-libs libaio ncurses-compat-libs
		fi
	fi
fi



if [ -f $tar_local ];then
	echo "$tar_local 文件存在"
else
	echo "$tar_local 文件不存在，退出脚本."
	exit
fi

# uncompress source file
tar -xvf ${tar_local} -C ${install_path}

ln -sv "${install_path}${tar_dir_name}" "${install_path}mysql"

chown -R root:root "${install_path}mysql"


# config env PATH

echo "PATH=${install_path}mysql/bin:\$PATH" > /etc/profile.d/mysql.sh




# prepare  data directory

[ -e ${data_path} ] || mkdir -pv "${data_path}"

chown -R mysql:mysql "${data_path}"


# prepare config file

cat > /etc/my.cnf <<EOF
[mysql]
socket=${data_path}mysql.sock
default-character-set=utf8mb4
[mysqld]
datadir=${data_path}
character-set-server=utf8mb4
socket=${data_path}mysql.sock
pid-file=${data_path}mysql.pid
# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0
slow_query_log = ON


# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#syslog
log-error=${data_path}mysql_log_error.log
pid-file=${data_path}mysql.pid
EOF

# init database and set root password is empty
cd ${install_path}mysql
./scripts/mysql_install_db --user=mysql --datadir=${data_path}
cd -
# config service

cat > /etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=mariadb Server
Documentation=https://mariadb.com/kb/en/library/systemd/
After=network.target

[Service]
User=mysql
Group=mysql
PIDFile=${data_path}mysql.pid
ExecStart=${install_path}mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
KillSignal=SIGTERM
SendSIGKILL=no
Restart=on-abort
RestartSec=5s
UMask=007
PrivateTmp=false
TimeoutStartSec=900
TimeoutStopSec=900
LimitNOFILE=32768
LimitMEMLOCK=524288
[Install]
WantedBy=multi-user.target


EOF

systemctl daemon-reload


systemctl start mysqld && echo "mysqld 启动成功!" || echo "mysqld 启动失败"

echo -e "#######\E[1;32m 已完成mariadb的安装和启动 \E[0m############"

echo -e  "初始密码为空，\E[1;31m必须修改初始密码！\E[0m
进入数据库命令:
source /etc/profile.d/mysql.sh
mysql -uroot 
修改初始密码sql语句如下:
alter user 'root'@'localhost' identified by '你的密码';
"





